// index.ts
import { AppContext } from "../../app.context";
import { UserInfo } from "../../mode/user";
import { LoginService } from "../../services/login.service";

Page({
  data: {
    userInfo: {} as UserInfo,
  },

  onLoad() {
    const userInfo = AppContext.instance.user.data;
    console.log(userInfo);
    if (userInfo) {
      this.setData({ userInfo })
    } else {

    }

  },
  onShow() {
    AppContext.instance.setTabbarActive(2);
    this.getTabBar().init();
  },

  getUserInfo() {
    LoginService.instance.getUserInfo().then((res: any) => {
      console.log(res);
    })
  },

  onLogin(): void {
    wx.reLaunch({
      url: '/pages/mine/login/login'
    })
  },

  logOut() {
    const that = this;
    wx.showModal({
      title: '提示',
      content: '确认退出吗',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '退出登录中...',
            mask: true
          })
          LoginService.instance.logOut().then((res: any) => {
            console.log(res);
            wx.showToast({
              title: '退出登录成功',
              icon: 'success'
            })
            that.setData({ userInfo: <UserInfo>{} });
            // setTimeout(() => {
            // wx.reLaunch({
            //   url: '/pages/mine/login/login'
            // })
            // }, 1300)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})
